home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / SPLASHWI.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  7KB  |  298 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.13  $
  6. //
  7. // Implementation of TSplashWindow
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_GDIOBJEC_H)
  11. # include <owl/gdiobjec.h>
  12. #endif
  13. #if !defined(OWL_LAYOUTWI_H)
  14. # include <owl/layoutwi.h>
  15. #endif
  16. #if !defined(OWL_PICTWIND_H)
  17. # include <owl/pictwind.h>
  18. #endif
  19. #if !defined(OWL_SPLASHWI_H)
  20. # include <owl/splashwi.h>
  21. #endif
  22. #if !defined(OWL_STATIC_H)
  23. # include <owl/static.h>
  24. #endif
  25. #if !defined(OWL_GAUGE_H)
  26. # include <owl/gauge.h>
  27. #endif
  28. #if !defined(WINSYS_UIMETRIC_H)
  29. # include <winsys/uimetric.h>
  30. #endif
  31.  
  32. OWL_DIAGINFO;
  33.  
  34. //
  35. // ID for creating a timer.
  36. //
  37. const int TimerId = 1;
  38.  
  39. //
  40. // Threshold value for removing the splash screen.
  41. //
  42. const int PercentThreshold = 95;
  43.  
  44. //
  45. // Response table for the splash window.
  46. //
  47. DEFINE_RESPONSE_TABLE1(TSplashWindow, TLayoutWindow)
  48.   EV_WM_LBUTTONDOWN,
  49.   EV_WM_TIMER,
  50. END_RESPONSE_TABLE;
  51.  
  52. //
  53. // Constructor to create a splash screen.
  54. // The parameters width and height are the dimensions of the window unless
  55. // the style ShrinkToFit is used. In which case, the size of the DIB is used.
  56. // The timeOut parameter is the number of milliseconds to wait until the
  57. // splash screen closes itself. Use 0 to not automatically close the the
  58. // splash screen.
  59. // The splash screen does NOT assume ownership of the DIB. The lifetime of
  60. // the DIB must be as long as the lifetime of the splash screen.
  61. //
  62. TSplashWindow::TSplashWindow(TDib& dib, int width, int height,
  63.   int style, uint timeOut, const char far* title, TModule* module)
  64. :
  65.   TLayoutWindow(0, title, module),
  66.   Style(style),
  67.   TimeOut(timeOut),
  68.   CapturedMouse(false),
  69.   Gauge(0),
  70.   PictWindow(0),
  71.   Static(0)
  72. {
  73.   if (HasStyle(ShrinkToFit)) {
  74.     Attr.W = dib.Width();
  75.     Attr.H = dib.Height();
  76.   }
  77.   else {
  78.     Attr.W = width;
  79.     Attr.H = height;
  80.   }
  81.  
  82.   Attr.Style = WS_POPUP | WS_VISIBLE | WS_BORDER;
  83.  
  84.   // If there's a title add the WS_CAPTION style. If ShrinkToFit is set
  85.   // we also want to adjust the height of the window to account for the
  86.   // caption.
  87.   //
  88.   if( title )  {
  89.     Attr.Style |= WS_CAPTION;
  90.     if( HasStyle(ShrinkToFit) )
  91.       Attr.H += ::GetSystemMetrics( SM_CYCAPTION );
  92.   }
  93.  
  94.   // Ready the layout metrics
  95.   //
  96.   int heightPercentDW = 100;
  97.   TLayoutMetrics lmPictWindow;
  98.   TLayoutMetrics lmGauge;
  99.   TLayoutMetrics lmStatic;
  100.  
  101.   // Center the dib by default unless ShrinktoFit is set.
  102.   //
  103.   if( HasStyle(ShrinkToFit) )
  104.     PictWindow = new TPictureWindow(this, &dib, TPictureWindow::UpperLeft, "", module);
  105.   else
  106.     PictWindow = new TPictureWindow(this, &dib, TPictureWindow::Center, "", module);
  107.  
  108.   lmPictWindow.X.SameAs(lmParent, lmLeft);
  109.   lmPictWindow.Y.SameAs(lmParent, lmTop);
  110.   lmPictWindow.Width.SameAs(lmParent, lmWidth);
  111.  
  112.   // Create optional static control
  113.   //
  114.   if (HasStyle(MakeStatic)) {
  115.     const int StaticPercent = 10;
  116.     Static = new TStatic(this, 1, "", 0, 0, 0, 0, 0, module);
  117.     Static->Attr.Style |= SS_CENTER;
  118.     heightPercentDW -= StaticPercent;
  119.     lmStatic.X.SameAs(lmParent, lmLeft);
  120.     lmStatic.Width.SameAs(lmParent, lmWidth);
  121.     lmStatic.Height.PercentOf(lmParent, StaticPercent, lmHeight);
  122.  
  123.     lmStatic.Y.Below(PictWindow, 1);
  124.     SetChildLayoutMetrics(*Static, lmStatic);
  125.   }
  126.  
  127.   // Create optional gauge control
  128.   //
  129.   if (HasStyle(MakeGauge)) {
  130.     const int GaugePercent = 10;
  131.     Gauge = new TGauge(this, "%d%%", 2, 0, 0, 0, 0, true, 0, module);
  132.     heightPercentDW -= GaugePercent;
  133.     lmGauge.X.SameAs(lmParent, lmLeft);
  134.     lmGauge.Width.SameAs(lmParent, lmWidth);
  135.     lmGauge.Height.PercentOf(lmParent, GaugePercent, lmHeight);
  136.  
  137.     if (HasStyle(MakeStatic))
  138.       lmGauge.Y.Below(Static, 1);
  139.     else
  140.       lmGauge.Y.Below(PictWindow, 1);
  141.  
  142.     SetChildLayoutMetrics(*Gauge, lmGauge);
  143.   }
  144.  
  145.   lmPictWindow.Height.PercentOf(lmParent, heightPercentDW, lmHeight);
  146.   SetChildLayoutMetrics(*PictWindow, lmPictWindow);
  147. }
  148.  
  149. //
  150. // Delete the child controls.
  151. //
  152. TSplashWindow::~TSplashWindow()
  153. {
  154.   delete Static;
  155.   delete Gauge;
  156.   delete PictWindow;
  157. }
  158.  
  159. //
  160. // After the window has been created, center the window and make it topmost.
  161. //
  162. void
  163. TSplashWindow::SetupWindow()
  164. {
  165.   TLayoutWindow::SetupWindow();
  166.  
  167.   // Center window, make topmost and adjust size to accomidate static
  168.   // and gauge.
  169.   //
  170.   TRect r  = GetWindowRect();
  171.  
  172.   if( HasStyle(MakeGauge) && HasStyle(MakeStatic) )  {
  173.     int deltaH = Gauge->Attr.H;
  174.     r.Inflate( 0, deltaH );
  175.   }
  176.  
  177.   TRect fullRect(0, 0, TUIMetric::CxScreen, TUIMetric::CyScreen);
  178.  
  179.   int x = (fullRect.Width() - r.Width()) / 2;
  180.   int y = (fullRect.Height() - r.Height()) / 2;
  181.   r.Offset(x, y);
  182.   SetWindowPos(HWND_TOPMOST, r, SWP_SHOWWINDOW);
  183.  
  184.   if (HasStyle(MakeGauge)) {
  185.     // Set the range
  186.     //
  187.     GetGauge()->SetRange(0, 100);
  188.   }
  189.  
  190.   if (GetTimeOut() != 0) {
  191.     // Create the timer
  192.     //
  193.     SetTimer(TimerId, GetTimeOut());
  194.   }
  195.  
  196.   // Trap the mouse click
  197.   //
  198.   if (HasStyle(CaptureMouse)) {
  199.     SetCapture();
  200.     SetCursor(0, IDC_ARROW);
  201.     CapturedMouse = true;
  202.   }
  203. }
  204.  
  205. //
  206. // Before the window closes, and if the mouse has been captured, release it now.
  207. //
  208. void
  209. TSplashWindow::CleanupWindow()
  210. {
  211.   if (CapturedMouse) {
  212.     ReleaseCapture();
  213.     CapturedMouse = false;
  214.   }
  215.  
  216.   if (GetTimeOut() != 0)
  217.     KillTimer(TimerId);
  218. }
  219.  
  220. //
  221. // Overload Create so that we can force the window to paint asap.
  222. //
  223. bool
  224. TSplashWindow::Create()
  225. {
  226.    bool retval = TLayoutWindow::Create();
  227.    UpdateWindow();
  228.    return retval;
  229. }
  230.  
  231. //
  232. // Change the text within the static control. If the splash screen does not
  233. // have a static control, this does not do anything.
  234. //
  235. void
  236. TSplashWindow::SetText(const char far* text)
  237. {
  238.   if (HasStyle(MakeStatic))
  239.     if (GetStatic()) {
  240.       GetStatic()->SetText(text);
  241.       GetApplication()->PumpWaitingMessages();
  242.     }
  243. }
  244.  
  245. //
  246. // Set the percentage done for the gauge control. If the splash screen does
  247. // not have a gauge control, this does not do anything.
  248. //
  249. void
  250. TSplashWindow::SetPercentDone(int percent)
  251. {
  252.   if (HasStyle(MakeGauge)) {
  253.     if (GetGauge()) {
  254.       GetGauge()->SetValue(percent);
  255.       GetApplication()->PumpWaitingMessages();
  256.     }
  257.  
  258.     if (percent > PercentThreshold) {
  259.       // Set up the timer
  260.       //
  261.       if (GetTimeOut() != 0)
  262.         SetTimer(TimerId, GetTimeOut());
  263.     }
  264.   }
  265. }
  266.  
  267. //
  268. // If the user clicks on the splash screen and the CaptureMouse style
  269. // is on, close the splash screen.
  270. //
  271. void
  272. TSplashWindow::EvLButtonDown(uint /*modKeys*/, TPoint& /*point*/)
  273. {
  274.   if (HasStyle(CaptureMouse)) {
  275.     if (HasStyle(MakeGauge))
  276.       if (GetGauge()->GetValue() < PercentThreshold)
  277.         return;
  278.       SendMessage(WM_CLOSE);
  279.   }
  280. }
  281.  
  282. //
  283. // Handler for the timer event.
  284. // Closes the window.
  285. //
  286. void
  287. TSplashWindow::EvTimer(uint /*timerId*/)
  288. {
  289.   if (HasStyle(MakeGauge)) {
  290.     if (GetGauge()->GetValue() < PercentThreshold) {
  291.       // If less than 90% and has a gauge, immediately return
  292.       //
  293.       return;
  294.     }
  295.   }
  296.   SendMessage(WM_CLOSE);
  297. }
  298.